//
//  LYExpendituresDetailController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/30.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYExpendituresDetailController.h"
#import "LYIncomBreakDownCell.h"
#import "LYRechargeController.h"

@interface LYExpendituresDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UILabel *balanceLabel;
@property (nonatomic, strong) UILabel *footLabel;

@property (nonatomic, assign) NSInteger page;

@end

@implementation LYExpendituresDetailController


-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"支出记录";
    self.page = 1;
    [self createSubview];
    [self requestData];
    [self pullUpRefresh];
}

- (void)createSubview{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT-HeightOfStatusBarAndNavigationBar) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    
    [self.tableview registerClass:[LYIncomBreakDownCell class] forCellReuseIdentifier:kCellIdentifier_LYIncomBreakDownCell];
    [self.view addSubview:self.tableview];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 130)];
    header.backgroundColor = [UIColor whiteColor];
    self.tableview.tableHeaderView = header;
    
    UIButton * recargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    recargeBtn.frame = CGRectMake(WIDTH - 115, 20, 100 , 30);
    recargeBtn.backgroundColor = [UIColor orangeColor];
    [recargeBtn setTitle:@"去充值" forState:UIControlStateNormal];
    [recargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    recargeBtn.layer.cornerRadius = recargeBtn.height/2;
    recargeBtn.clipsToBounds = YES;
    recargeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [recargeBtn addTarget:self action:@selector(recargeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:recargeBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, recargeBtn.left - 20, 20)];
    titleLabel.text = @"我的余额（元）";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = RGB(34, 34, 34);
    [header addSubview:titleLabel];
    
    self.balanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.bottom +5, titleLabel.width, 30)];
    self.balanceLabel.text =  [NSString stringWithFormat:@"%@元",self.amount];;
    self.balanceLabel.font = [UIFont boldSystemFontOfSize:28];
    self.balanceLabel.textColor = [UIColor orangeColor];
    [header addSubview:self.balanceLabel];
    
    UIView *glayView = [[UIView alloc] initWithFrame:CGRectMake(0, self.balanceLabel.bottom +4,WIDTH , 6)];
    glayView.backgroundColor = RGB(247, 247, 247);
    [header addSubview:glayView];
    
    UILabel *recordLabel = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 90)/2, glayView.bottom +10, 90, 20)];
    recordLabel.text = @"相关支出记录";
    recordLabel.textAlignment = NSTextAlignmentCenter;
    recordLabel.font = [UIFont systemFontOfSize:13];
    recordLabel.textColor = RGB(85, 85, 85);
    [header addSubview:recordLabel];
    
    UIView *leftline = [[UIView alloc] initWithFrame:CGRectMake(recordLabel.left -75, recordLabel.top+9.5, 60, 1)];
    leftline.backgroundColor = RGB(247, 247, 247);
    [header addSubview:leftline];
    
    UIView *rightline = [[UIView alloc] initWithFrame:CGRectMake(recordLabel.right +15, recordLabel.top+9.5, 60, 1)];
    rightline.backgroundColor = RGB(247, 247, 247);
    [header addSubview:rightline];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, leftline.bottom +10, 90, 20)];
    nameLabel.text = @"名称";
    nameLabel.font = [UIFont boldSystemFontOfSize:13];
    nameLabel.textColor = RGB(34, 34, 34);
    [header addSubview:nameLabel];
    
    self.footLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 45)];
    self.footLabel.backgroundColor = [UIColor whiteColor];
    self.footLabel.font = [UIFont systemFontOfSize:14];
    self.footLabel.textColor = RGB(85, 85, 85);
//    self.footLabel.text = @"累积支出100.65元";
    self.footLabel.textAlignment = NSTextAlignmentCenter;
    self.tableview.tableFooterView = self.footLabel;
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYIncomBreakDownCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYIncomBreakDownCell forIndexPath:indexPath];
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

#pragma mark-- loadData
- (void)pullUpRefresh{
    //    //上啦加载
    __weak typeof(self) weakSelf = self;
    MJRefreshAutoNormalFooter *footer =  [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf pullUp];
    }];
    footer.stateLabel.textColor = RGB(153, 153, 153);
    self.tableview.mj_footer = footer;
}

- (void)pullUp{
    self.page ++;
    [self requestData];
}

- (void)requestData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    //1 收入 2支出
    dic[@"typeid"] = @2;
    dic[@"page"] = [NSNumber numberWithInteger:self.page];
    dic[@"number"] = @10;
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Wallet_getAccountLogs_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            //收入信息信息
            [weakSelf organizeData:responseObject[@"data"]];
        }else{
            SVPHUDError(responseObject[@"msg"]);
            [self pullUpFail];
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
        [self pullUpFail];
    }];
}

/**
 上拉失败，pagenum不增加
 */
- (void)pullUpFail{
    //上拉
    if (self.page > 1) {
        self.page --;
    }
    [self.tableview.mj_footer endRefreshing];
}


- (void)organizeData:(NSDictionary *)dataDic{
    self.footLabel.text = [NSString stringWithFormat:@"累积支出%@元",dataDic[@"total"]];
    NSArray *array = dataDic[@"logs"];
    if (array.count == 0) {
        [self.tableview.mj_footer endRefreshingWithNoMoreData];
    }else{
        for (NSDictionary *dic in array) {
            [self.dataArray addObject:dic];
        }
        [self.tableview.mj_footer endRefreshing];
    }
    [self.tableview reloadData];
}

#pragma mark -- Acrion
- (void)recargeBtnClick:(UIButton *)button{
    LYRechargeController *regchargeVC = [[LYRechargeController alloc] init];
    [self.navigationController pushViewController:regchargeVC animated:YES];
}



@end
